<?php

/**
 * SystemController.class.php 后台应用管理基类
 * Created Time 2016年3月10日 下午5:58:48
 *
 * @author     codingyuan@163.com
 */
namespace System\Common;

use Common\Common\CommonController;

class SystemController extends CommonController {
    protected $system_title = '后台管理';
    protected $system_left_tpl = 'System@Public:left';
    protected $system_left_menu = array();
    protected $app_menu = array();

    public function loadSystemView($templateFile = '', $charset = '', $contentType = '', $content = '', $prefix = '')
    {

        $this->assign ('system_title', $this->system_title);
        $this->assign('current_app', $this->identifier);
        $this->assign('is_login', true);
        $this->assign('leftmenu', $this->system_left_menu);
        
        // 设置app菜单
        $this->_getMenu();
        if($this->app_menu)
        {
            $this->assign('app_menu', $this->app_menu);
        }
        
        // 设置后台模块模板
        $this->display ('System@Public:header', $charset, $contentType, $content, $prefix);
        $this->display ($this->system_left_tpl, $charset, $contentType, $content, $prefix);
        $this->display ('System@Public:right', $charset, $contentType, $content, $prefix);
        $this->display ($templateFile, $charset, $contentType, $content, $prefix);
        $this->display ('System@Public:footer', $charset, $contentType, $content, $prefix);
    }
    
    protected function _getMenu()
    {
        $menu = array();
        $menuLink = array();
        $module = 'System';
        $ctl = 'Index';
        $act = 'index';
        $tmpMenu = C('menu', null, array());
        if($tmpMenu)
        {
            foreach ($tmpMenu as $val)
            {
                if($val['display'] && $val['method'] == 'get')
                {
                    if(isset($val['params']) && is_array($val['params']))
                    {
                        $params = $val['params'];
                    }
                
                    if(isset($val['module']))
                    {
                        $module = $val['module'];
                    }
                
                    if(isset($val['ctl']))
                    {
                        $ctl = $val['ctl'];
                    }
                
                    if(isset($val['act']))
                    {
                        $act = $val['act'];
                    }
                    $domain = C('domain', null, '');
                    $url = sprintf('%s/%s/%s@%s', $module, $ctl, $act, $domain);
                    $menuLink['url'] = U($url,$params);
                    $menuLink['label'] = isset($val['label']) ? $val['label'] : 'default lable';
                    $menuLink['act'] = $act;
                    $menuLink['icon'] = isset($val['icon']) ? $val['icon'] : 'glyphicon-certificate';
                    $menu[] = $menuLink;
                }
            }
        }
        $count = count($menu);
        if($count >4)
        {
            $tmpArr['display'] = array_slice($menu, 0, 4);
            $tmpArr['submenu'] = array_slice($menu, 4);
        }else
        {
            $tmpArr['display'] = $menu;
        }
//         echo '<pre>';
//         var_dump($menu);
//         exit;
        $this->app_menu = $tmpArr;
    }

    protected function _assignUeditor($defaultContent='', $contentKey='content', $options = array(), $toolBars = '_mobile', $innerHight = 300)
    {
        $ueditor = \Component\Common\Ueditor::get_instance($options);
        // $contentKey = 'content';
        $ueditor->ueditor_config = array('toolbars' => $toolBars, 'textarea' => $contentKey, 'initialFrameHeight' => $innerHight, 'initialContent' =>$defaultContent, 'elementPathEnabled' => false);
        if (! $ueditor->create_editor ('content'))
        {save_app_log($ueditor->ueditor_error, $this->identifier);}
        $ueditor_html = $ueditor->ueditor_html;
        $this->assign ('ueditor', $ueditor_html);
    
        return true;
    }
    
    protected function _assignPage($count, $limit, $filter=array())
    {
        $pager = new \Think\Page($count, $limit);
        
        if($filter)
        {
            foreach($filter as $key=>$val) {
                $pager->parameter[$key]   =   $val;
            }
        }
        
        $this->assign('pager', $pager->show());
        return true;
    }
    
    private function __getTopMenu()
    {
        $topMenu = array();
        $params = array();
        $module = 'System';
        $ctl = 'Index';
        $act = 'index';
        $topArr = C('menu.top_system_menu', null, array());
        foreach ($topArr as $val)
        {
            
        }
        
    }
    

}